package com.sidom.movieIndexer.domain.file;

import static org.apache.commons.io.FilenameUtils.removeExtension;
import static org.apache.commons.lang.Validate.notNull;

import java.io.File;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;

/**
 * A class representing of a MovieFile.
 * 
 * @author Siebe
 */
public class MovieFile {
	/**
	 * The file name without extension.
	 * e.g. swordfish.avi -> swordfish.
	 */
	private String fileNameWithoutExtension;
	/**
	 * The file name without extension. 
	 * Cleaned by the cleanupService.
	 * e.g. Swordfish CD1 -> Swordfish
	 */
	private String cleanedFileName;
	/**
	 * The {@link File} on the disk.
	 */
	private File file;
	/**
	 * The path to the parent directory.
	 * e.g. \movies\swordfish -> \movies
	 */
	private String parentPath;
	
	/**
	 * Constructor
	 * @param file The file on the disk.
	 */
	public MovieFile(File file){
		notNull(file, "file should not be null");
		notNull(file.getAbsoluteFile(), "file.getAbsoluteFile() should not be null");
		notNull(file.getAbsoluteFile().getParentFile(), "file.getAbsoluteFile().getParentFile() should not be null");
		this.file = file;
		this.fileNameWithoutExtension = removeExtension(file.getName());
		this.parentPath = file.getAbsoluteFile().getParentFile().getAbsolutePath();
	}
	
	/**
	 * @return the fileNameWithoutExtension
	 */
	public String getFileNameWithoutExtension() {
		return fileNameWithoutExtension;
	}

	/**
	 * @return the cleanedFileName
	 */
	public String getCleanedFileName() {
		return cleanedFileName;
	}

	/**
	 * @param cleanedFileName the cleanedFileName to set
	 */
	public void setCleanedFileName(final String cleanedFileName) {
		this.cleanedFileName = cleanedFileName;
	}

	/**
	 * @return the file
	 */
	public File getFile() {
		return file;
	}

	/**
	 * @return the parentPath
	 */
	public String getParentPath() {
		return parentPath;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public final int hashCode() {
		return HashCodeBuilder.reflectionHashCode(this);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public final boolean equals(final Object obj) {
		if(obj==this){
			return true;
		}
		if(!(obj instanceof MovieFile)){
			return false;
		}
		MovieFile movieFileToCheck = (MovieFile)obj;
		return EqualsBuilder.reflectionEquals(this, movieFileToCheck);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public final String toString() {
		return ReflectionToStringBuilder.toString(this);
	}
}
